/*
 * class name: stdui,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: core.js,
 * update: http://jsease.googlecode.com/svn/trunk/stdui.js
 * -------
 * todo: bind keys event
 */
function stdui(){
    var A = arguments;

    this.opt = ext( {
                    menu : {
                        id : 'xmenu',
                        height : 25
                    },
                    main : {
                        id : 'xbody',
                        bgcolor : '#f9f9f9'
                    },
                    status : {
                        id : 'xstatus',
                        height : 28,
                        bgcolor : '#e3edf9',
                        ttl : 60
                    },
                    nomenu : 0,
                    nostatus : 0
                }, A[0], 1 );

    this.uitype = 'stdui';

    var _ = this;

    _.statusoff = function () {
        _.status.style.display = 'none';

        $$.html.style.cssText = 'padding:0 !important;padding:'
            + (_.opt.nomenu ? 0 : _.opt.menu.height) +'px 0 0 0';

        _.main.style.bottom = 0;

        _.opt.status.off = true;

        if ( typeof _.opt.status.timer == 'number' ) {
            clearTimeout( _.opt.status.timer );
            delete _.opt.status['timer'];
        }

    return _;
    };

    _.statuson = function () {
        _.status.style.display = '';

        $$.html.style.cssText = 'padding:0 !important;padding:'
            + (_.opt.nomenu ? 0 : _.opt.menu.height) +'px 0 '
            + (_.opt.nostatus ? 0 : _.opt.status.height) +'px 0;';

        _.main.style.bottom = (_.opt.nostatus ? 0 : _.opt.status.height) +'px';

        _.opt.status.off = false;

        _.autohide();

    return _;
    };

}

stdui.prototype = {
    init : function() {
        $$.html.style.cssText = 'padding:0 !important;padding:'
            + (this.opt.nomenu ? 0 : this.opt.menu.height) +'px 0 '
            + (this.opt.nostatus ? 0 : this.opt.status.height) +'px 0;';

        //menu
        if ( !this.opt.nomenu ) {
            this.menu = elm( 'div' ).before();
            this.menu.id = this.opt.menu.id;

            this.menu.style.cssText = 'position:absolute;top:0;left:0;width:100%;height:'+this.opt.menu.height +'px;';
        }

        //main div
        this.main = elm( 'div' ).after( this.menu || me.body );
        this.main.id = this.opt.main.id;
        this.main.style.cssText = 'height:auto !important;height:100%;position:absolute;top:'+
            (this.opt.nomenu ? 0 : this.opt.menu.height) +'px;bottom:'+
            (this.opt.nostatus ? 0 : this.opt.status.height) +
            'px;left:0;right:0;width:100%;overflow:auto;background-color:'+this.opt.main.bgcolor;

        //status
        if ( !this.opt.nostatus ) {
            this.status = elm( 'div' ).after( this.main || me.body );
            this.status.id = this.opt.status.id;
            this.status.style.cssText = 'position:absolute;bottom:0;left:0;width:100%;height:'+
                (this.opt.status.height-1) +'px;line-height:'+(this.opt.status.height-1)+
                'px;text-align:center;border-top:solid #add8e6 1px;background-color:'+this.opt.status.bgcolor;
            this.autohide();
        }

    return this;
    },

    autohide : function () {
        if ( !this.opt.status.off ) {
            if ( typeof this.opt.status.timer == 'number' ) {
                clearTimeout( this.opt.status.timer );
                delete this.opt.status['timer'];
            }
            this.opt.status.timer = setTimeout(this.statusoff, this.opt.status.ttl*1000);
        } else {
            this.statuson();
        }
    return this;
    },

    getres : function() {
        var A = arguments;

        this.opt.res = ext( { url : '', std : true },  A[0] );

        if ( p( this.opt.res, 'url' ) ) {
            this.xml = loadXml( this.opt.res.url );
            this.$ = jsonDom( this.xml, this.opt.res.std );

            if ( typeof this.opt.res.fn == 'function' ) {
                this.opt.res.fn(this.$);
            }
        }

    return this;
    }

}
